//
//  KATLoadingAnimationView.m
//  KATFramework
//
//  Created by Kantice on 16/6/22.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATLoadingAnimationView.h"



@implementation KATLoadingAnimationView

#pragma -mark 类方法

///获取实例
+ (instancetype)animationWithFrame:(CGRect)frame
{
    KATLoadingAnimationView *anim=[[[self alloc] init] autorelease];
    
    anim.frame=frame;
    anim.backgroundColor=[UIColor clearColor];
    anim.ringCut=1.0/12.0;
    anim.cycleDuration=1.0;
    
    //环
    anim.ringShape=[KATShapeUtil arcShapeWithRadius:frame.size.height*0.5 startAngle:M_PI*3.5 endAngle:M_PI*1.5 clockwise:NO];
    anim.ringShape.fillColor=[UIColor clearColor].CGColor;
    anim.ringShape.frame=anim.bounds;
    [anim.layer addSublayer:anim.ringShape];
    
    //成功
    anim.successShape=[KATShapeUtil checkShapeWithWidth:frame.size.height*0.62 andHeight:frame.size.height*0.62];
    anim.successShape.fillColor=[UIColor clearColor].CGColor;
    anim.successShape.frame=CGRectMake(frame.size.height*0.19, frame.size.height*0.19, anim.successShape.bounds.size.width, anim.successShape.bounds.size.height);
    [anim.layer addSublayer:anim.successShape];
    
    //失败
    anim.failShape=[KATShapeUtil crossShapeWithWidth:frame.size.height*0.5 andHeight:frame.size.height*0.5];
    anim.failShape.fillColor=[UIColor clearColor].CGColor;
    anim.failShape.frame=CGRectMake(frame.size.height*0.25, frame.size.height*0.25, anim.failShape.bounds.size.width, anim.failShape.bounds.size.height);
    [anim.layer addSublayer:anim.failShape];
    
    [anim restore];
    
    
    return anim;
}


#pragma -mark 对象方法

//复位
- (void)restore
{
    [_ringShape clearAnimations];
    [_ringShape restoreShape];
    _ringShape.strokeStart=0;
    _ringShape.strokeEnd=0;
    
    [_successShape clearAnimations];
    [_successShape restoreShape];
    _successShape.strokeStart=0;
    _successShape.strokeEnd=0;
    
    [_failShape clearAnimations];
    [_failShape restoreShape];
    _failShape.strokeStart=0;
    _failShape.strokeEnd=0;
    
    _status=LOADING_ANIMATION_STATUS_READY;
}


//成功
- (void)success
{
    if(_status==LOADING_ANIMATION_STATUS_START || _status==LOADING_ANIMATION_STATUS_STOP)
    {
        [_ringShape clearAnimations];
        [_ringShape startAnimation:[_ringShape animationStrokeEndToValue:1.0 withDuration:(1.0-_ringCut-_ringShape.strokeEnd)*_cycleDuration*0.25+_ringCut*_cycleDuration*0.5 repeat:1 autoreverses:NO delay:0] andDelay:0];
        
        [_successShape clearAnimations];
        [_successShape startAnimation:[_successShape animationStrokeEndToValue:1.0 withDuration:_cycleDuration*0.25 repeat:1 autoreverses:NO delay:0] andDelay:0];
        
        _status=LOADING_ANIMATION_STATUS_FINISH;
    }
}


//失败
- (void)fail
{
    if(_status==LOADING_ANIMATION_STATUS_START || _status==LOADING_ANIMATION_STATUS_STOP)
    {
        [_ringShape clearAnimations];
        [_ringShape startAnimation:[_ringShape animationStrokeEndToValue:1.0 withDuration:(1.0-_ringCut-_ringShape.strokeEnd)*_cycleDuration*0.25+_ringCut*_cycleDuration*0.5 repeat:1 autoreverses:NO delay:0] andDelay:0];
        
        [_failShape clearAnimations];
        [_failShape startAnimation:[_failShape animationStrokeEndToValue:1.0 withDuration:_cycleDuration*0.25 repeat:1 autoreverses:NO delay:0] andDelay:0];
        
        _status=LOADING_ANIMATION_STATUS_FINISH;
    }
}



//播放
- (void)play
{
    if(_status!=LOADING_ANIMATION_STATUS_START)
    {
        [self restore];
        
        [_ringShape startAnimation:[_ringShape animationStrokeEndToValue:1.0-_ringCut withDuration:(1.0-_ringCut)*_cycleDuration repeat:1 autoreverses:NO delay:0] andDelay:0];
        [_ringShape rotateWithDuration:_cycleDuration repeat:-1 clockwise:NO];
        
        _status=LOADING_ANIMATION_STATUS_START;
    }
}


//停止
- (void)stop
{
    if(_status==LOADING_ANIMATION_STATUS_START)
    {
        [_ringShape stopAnimation];
        
        _status=LOADING_ANIMATION_STATUS_STOP;
    }
}


//重新播放
- (void)replay
{
    [self stop];
    
    [self play];
}


//完成
- (void)finishWithValue:(KATAnimationFinishValue)value
{
    if(value==KATAnimationFinishValueSuccess)
    {
        [self success];
    }
    else if(value==KATAnimationFinishValueFail)
    {
        [self fail];
    }
    else
    {
        [self stop];
    }
}


//释放内存
- (void)dealloc
{
    [_ringShape release];
    [_successShape release];
    [_failShape release];
    
    [super dealloc];
}

@end
